package com.yyz.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yyz.domain.Category;
import com.yyz.common.R;
import com.yyz.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yyz
 * @create 2023-07-20 14:54
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 添加菜品分类
     *
     * @param category
     * @return
     */
    @PostMapping
    public R insert(@RequestBody Category category) {
        categoryService.insert(category);

        return R.success("添加成功！");
    }

    /**
     * 分页查询菜品信息
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R pageList(Integer page, Integer pageSize) {
        log.info("page:{},pageSize:{}", page, pageSize);
        IPage<Category> pageInfo = categoryService.pageList(page, pageSize);

        return R.success(pageInfo);
    }

    /**
     * 修改菜品
     *
     * @param category
     * @return
     */
    @PutMapping
    public R update(@RequestBody Category category) {
        categoryService.update(category);

        return R.success("修改成功！");
    }

    /**
     * 删除分类
     *
     * @param ids
     */
    @DeleteMapping
    public R delete(Long ids) {
        log.info("id:{}", ids);

        categoryService.delete(ids);

        return R.success("删除成功！");
    }

    /**
     * 菜品分类下拉框
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R list(Category category){
        List<Category> list = categoryService.list(category);

        return R.success(list);
    }
}
